//
//  HBSimpleTableViewCell.m
//  FMSManager
//
//  Created by hongbao.cui on 15-2-2.
//  Copyright (c) 2015年 hongbao.cui. All rights reserved.
//

#import "HBSimpleTableViewCell.h"
#import "ApplicationUtils.h"
@implementation HBSimpleTableViewCell
-(void)dealloc{
    if (self.InputBlock) {
        self.InputBlock = nil;
    }
    if (self.DraftBlock) {
        self.DraftBlock = nil;
    }
    if (self.PhotoBlock) {
        self.PhotoBlock = nil;
    }
    if (self.textViewBlock) {
        self.textViewBlock = nil;
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setBtnProperty:(UIButton *)sender{
    [_assignBtn setSelected:NO];
    [sender setSelected:YES];
    _assignBtn = sender;
}
-(void)textBtnClicked:(UIButton *)sender{
    [self setBtnProperty:sender];
    [self initInputView];
    if (self.InputBlock) {
        self.InputBlock(self,_inputTextView.text);
    }
}
-(void)draftBtnClicked:(UIButton *)sender{
    [self setBtnProperty:sender];
    [self initDraftView];
    if (self.DraftBlock) {
        self.DraftBlock(sender);
    }
}
-(void)photoBtnClicked:(UIButton *)sender{
    [self setBtnProperty:sender];
    [self initPhotoView];
    if (self.PhotoBlock) {
        self.PhotoBlock(sender);
    }
}
-(void)selfRemoveFromSuperView{
    if (_inputTextView) {
        for (UIView *subView in [_inputTextView subviews]) {
            [subView removeFromSuperview];
        }
        [_inputTextView removeFromSuperview];
        _inputTextView = nil;
    }
    if (_draftImageView) {
        for (UIView *subView in [_draftImageView subviews]) {
            [subView removeFromSuperview];
        }
        [_draftImageView removeFromSuperview];
        _draftImageView = nil;
    }
    if (_photoImageView) {
        for (UIView *subView in [_photoImageView subviews]) {
            [subView removeFromSuperview];
        }
        [_photoImageView removeFromSuperview];
        _photoImageView = nil;
    }
}
-(void)initInputView{
    [self selfRemoveFromSuperView];
    if (!_inputTextView) {
        _inputTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, 25, FMSCREENWIDTH-40, FMSCREENHEIGHT/3)];
        _inputTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _inputTextView.layer.borderWidth = 1;
        _inputTextView.layer.cornerRadius = 10;
    }
    if (!_placeholderLabel) {
        _placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 25)];
        [_placeholderLabel setText:@"请输入答案..."];
        [_placeholderLabel setTextColor:[UIColor lightGrayColor]];
    }
    [_inputTextView addSubview:_placeholderLabel];
    _inputTextView.text = @"";
    [_inputTextView setReturnKeyType:UIReturnKeyDone];
    [_inputTextView setTextContainerInset:UIEdgeInsetsMake(15, 10, 0, 0)];
    [_inputTextView setDelegate:self];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[_inputTextView bounds]];
    UIImage *bgImage = [[UIImage imageNamed:@"Exercises-bj.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 40, 10, 10)];
    imageView.image = bgImage;
    [_inputTextView addSubview:imageView];
    [_inputTextView sendSubviewToBack:imageView];
    [self addSubview:_inputTextView];
}
-(void)longPress:(UILongPressGestureRecognizer *)gesture{
    UIImageView *imageView = (UIImageView *)[gesture view];
//    UIImage *photobgImage = [[UIImage imageNamed:@"Exercises-bj.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 40, 10, 10)];
    imageView.image = nil;
    //照相重新拍摄删除之前的图片
    NSString *pathOld = [NSString stringWithFormat:@"%@/%@/image/%@",FMPAPERSAVE_PATH,[_dataInfoDict objectForKey:@"paper_id"],[_dataInfoDict objectForKey:@"user_answer"]];
    BOOL oldImageFile = [ApplicationUtils deleteExitsFile:pathOld];
    if (oldImageFile) {
        FMSCHBLog(@"文件删除成功");
    }else{
        FMSCHBLog(@"文件删除失败");
    }
}
-(void)initDraftView{
    [self selfRemoveFromSuperView];
    if (!_draftImageView) {
        _draftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 25, FMSCREENWIDTH-40, FMSCREENHEIGHT/3)];
    }
    UIImageView *draftBgView = [[UIImageView alloc] initWithFrame:[_draftImageView bounds]];
//    UIImage *draftbgImage = [[UIImage imageNamed:@"Exercises-bj.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 40, 10, 10)];
//    draftbgImage = [draftbgImage resizableImageWithCapInsets:UIEdgeInsetsMake(20, 10, 10, 100)];
//    draftBgView.image = draftbgImage;
    draftBgView.tag = 100;
    [draftBgView setUserInteractionEnabled:YES];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration = 1.0;
    [draftBgView addGestureRecognizer:longPress];
    [_draftImageView addSubview:draftBgView];
    [_draftImageView setUserInteractionEnabled:YES];
    [self addSubview:_draftImageView];
}
-(void)initPhotoView{
    [self selfRemoveFromSuperView];
    if (!_photoImageView) {
        _photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 25, FMSCREENWIDTH-40, FMSCREENHEIGHT/3)];
    }
    UIImageView *photoBgView = [[UIImageView alloc] initWithFrame:[_photoImageView bounds]];
//    UIImage *photobgImage = [[UIImage imageNamed:@"Exercises-bj.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 40, 10, 10)];
//    photoBgView.image = photobgImage;
    photoBgView.tag = 100;
    [photoBgView setUserInteractionEnabled:YES];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration = 2.0;
    [photoBgView addGestureRecognizer:longPress];
    [_photoImageView setUserInteractionEnabled:YES];
    [_photoImageView addSubview:photoBgView];
    [self addSubview:_photoImageView];
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _textBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_textBtn setBackgroundImage:[UIImage imageNamed:@"word-normal.png"] forState:UIControlStateNormal];
        [_textBtn setBackgroundImage:[UIImage imageNamed:@"word-hover.png"] forState:UIControlStateHighlighted];
        [_textBtn setBackgroundImage:[UIImage imageNamed:@"word-hover.png"] forState:UIControlStateSelected];
        [_textBtn setSelected:YES];
        _assignBtn = _textBtn;
        [_textBtn setFrame:CGRectMake(25, 0, 24.0, 22.0)];
        [_textBtn addTarget:self action:@selector(textBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_textBtn];
        
        _draftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_draftBtn setBackgroundImage:[UIImage imageNamed:@"painter-normal.png"] forState:UIControlStateNormal];
        [_draftBtn setBackgroundImage:[UIImage imageNamed:@"painter-hover.png"] forState:UIControlStateHighlighted];
        [_draftBtn setBackgroundImage:[UIImage imageNamed:@"painter-hover.png"] forState:UIControlStateSelected];
        [_draftBtn setFrame:CGRectMake(60, 0, 23.0, 23.0)];
        [_draftBtn addTarget:self action:@selector(draftBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_draftBtn];
        
        
        _photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_photoBtn setBackgroundImage:[UIImage imageNamed:@"photo-normal.png"] forState:UIControlStateNormal];
        [_photoBtn setBackgroundImage:[UIImage imageNamed:@"photo-hover.png"] forState:UIControlStateHighlighted];
        [_photoBtn setBackgroundImage:[UIImage imageNamed:@"photo-hover.png"] forState:UIControlStateSelected];
        [_photoBtn setFrame:CGRectMake(93, 0, 23.0, 18.0)];
        [_photoBtn addTarget:self action:@selector(photoBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_photoBtn];
//        [self initInputView];
        [self textBtnClicked:_textBtn];
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    return self;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    _placeholderLabel.hidden = YES;
    if (self.textViewBlock) {
        self.textViewBlock(textView);
    }
    return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
//    if (self.InputBlock) {
//        self.InputBlock(_inputTextView.text);
//    }
    if (textView.text.length>0) {
        return;
    }
    _placeholderLabel.hidden = NO;
}
- (void)textViewDidChange:(UITextView *)textView{
    if (self.InputBlock) {
        self.InputBlock(self,_inputTextView.text);
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (![text isEqualToString:@""])
      {
            _placeholderLabel.hidden = YES;
    }
    
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
        
    {
        if (![textView isFirstResponder]) {
            _placeholderLabel.hidden = NO;
        }
    }
    if ([text isEqualToString:@"\n"]) {
        if (self.InputBlock) {
            self.InputBlock(self,_inputTextView.text);
        }
        [textView resignFirstResponder];
    }
    return YES;
    
}
@end
